package lily.phs.child;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lily.phs.database.Constants;
import lily.phs.entertain.YuleActivity;
import lily.phs.epidemic.EpidemicList;
import lily.phs.healthedu.HealthMain;
import lily.phs.object.ModuleSelect;
import lily.phs.pregnance.PregnantScrollActivity;
import lily.phs.urh.PeopleScrollActivity;
import lily.phs.urh.R;
import lily.phs.vaccination.VaccinationScrollActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author chen
 *当选择某一个儿童进行查看该儿童0~6岁儿童健康档案管理规范时，该类用于
 *显示该儿童的
 *1新生儿家庭访视记录表，2.1岁以内儿童健康检查记录表，3.1～2岁儿童健康检查记录表，4.3～6岁儿童健康检查记录表
 *这四张表的详细内容
 */
public class ChildScrollActivity extends Activity implements OnClickListener,Constants{
	private static final String TAG = "IsmsMainActivity";
	public static final String FIRST_INTENT_TAG = "first";
	public static final String SECOND_INTENT_TAG = "second";
	public static final String THIRD_INTENT_TAG = "third";
	public static final String FOUR_INTENT_TAG = "four";
	public static final String FIVE_INTENT_TAG = "five";
	public static final String SIX_INTENT_TAG = "six";
	public static final String SEVEN_INTENT_TAG = "seven";
	public static final String EIGHT_INTENT_TAG = "eight";
	public static final int FIRST_VIEW = 0;
	public static final int SECOND_VIEW = 1;
	public static final int THIRD_VIEW = 2;
	public static final int FOUR_VIEW = 3;
	public static final int FIVE_VIEW = 4;
	public static final int SIX_VIEW = 5;
	public static final int SEVEN_VIEW = 6;
	public static final int EIGHT_VIEW = 7;
	public static final String[] childTable={"新生儿家庭访视记录表","1岁以内儿童健康检查记录表","1~2岁儿童健康检查记录表","3~6岁儿童健康检查记录表"};
	ScrollLayout mRoot;
	private int profileid,father,call;
	Button leftBtn;
    Button rightBtn ;
    Button rightBtn2;
    private int pageIndex;
   
    NewChildBornView newChildBornView;
	NewChildBornView_Two newChildBornViewTwo;
	SmallOneChildView smallOneChildView;
	SmallOneChildView_Two smallOneChildViewTwo;
	OneToTwoChildView oneToTwoChildView;
	OneToTwoChildView_Two oneToTwoChildViewTwo;
	ThreeToSixChildView threeToSixChildView;
	ThreeToSixChildView_Two threeToSixChildViewTwo;
	
	private ProgressDialog mpDialog;
	
	List<String> accountFather;
	ListView listAccount;
	
	 private static final String[] nameOfModuleList = {
	    	"城乡居民健康档案管理","健康教育","疫情上报","卫生监督","寓教于乐"};
	 private static final int[] imageOfModuleList = {
	    	R.drawable.peoplehealth,R.drawable.health_education,R.drawable.yiqingshangbao,
	    	R.drawable.weishengjiandu,R.drawable.entertain};
	
	int image[];
	String name[];
	GridView gv2;
    
    /**
     * 当用户在界面进行滑动，选择某一张表时，回在该Handler中返回选择的具体页面的编号
     */
 /*   public Handler mHandler = new Handler(){
    	@Override
    	public void handleMessage(Message msg) {
    		
    		super.handleMessage(msg);
    		switch (msg.what) {
			case FIRST_VIEW:
				pageIndex=0;
				break;
			case SECOND_VIEW:
				pageIndex=1;
				break;
				
			case THIRD_VIEW:
				pageIndex=2;
				break;
			case FOUR_VIEW:
				pageIndex=3;
				break;
			case FIVE_VIEW:
				pageIndex=4;
				break;
				
			case SIX_VIEW:
				pageIndex=5;
				break;
			case SEVEN_VIEW:
				pageIndex=6;
				break;
			case EIGHT_VIEW:
				pageIndex=7;
				break;
			default:
				break;
			}
    		System.out.println("pageIndex="+pageIndex);
    	}
    };*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //mLocalActivityManager = getLocalActivityManager();
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
        leftBtn = (Button) findViewById(R.id.title_left_btn);
        rightBtn = (Button) findViewById(R.id.title_right_btn);
        rightBtn2 = (Button) findViewById(R.id.title_right_btn2);
        leftBtn.setText("后退");
        rightBtn.setText("保存");
        rightBtn2.setText("提交");
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        rightBtn2.setOnClickListener(this);
        mRoot = (ScrollLayout) findViewById(R.id.root);
        listAccount = (ListView) findViewById(R.id.listAccount);
        initExList(); 

        Intent i=this.getIntent();
        Bundle bd=i.getExtras();
        profileid=bd.getInt("Profile_id");
        father=bd.getInt("father");
		call=bd.getInt("call");
        initView();
        mRoot.setToScreen(0);
        pageIndex=0;
        listAccount.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				// TODO Auto-generated method stub
				System.out.println("arg2="+index);
				switch(index)
				{
			
				case 0:
					mRoot.setToScreen(0);
					break;
				
				case 1:
					mRoot.setToScreen(2);
					break;
					
        		case 2:
        			mRoot.setToScreen(3);
        			break;
				
        		case 3:
        			mRoot.setToScreen(4);
        			break;
		
			//	System.out.println("pageIndex2="+pageIndex);
				}
			}
        });
        
        gv2=(GridView)findViewById(R.id.gride2);
        ImageList adapter2 = new ImageList(this,imageOfModuleList,nameOfModuleList);
		gv2.setAdapter(adapter2);
		gv2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:{//健康档案
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileid);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_VACCINATION);
						editprofile.putExtras(bd);
						editprofile.setClass(ChildScrollActivity.this, PeopleScrollActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_PEOPLE_HEALTH){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileid);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(ChildScrollActivity.this, PeopleScrollActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 1:{//健康教育
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileid);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(ChildScrollActivity.this, HealthMain.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileid);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(ChildScrollActivity.this, HealthMain.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 2:{//疫情上报
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileid);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(ChildScrollActivity.this, EpidemicList.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileid);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(ChildScrollActivity.this, EpidemicList.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 3:{//卫生监督
					
					break;
				}
				case 4:{//寓教于乐
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileid);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(ChildScrollActivity.this, YuleActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileid);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(ChildScrollActivity.this, YuleActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
					
				default:
					break;
				}
			}
		});
        
        mpDialog = new ProgressDialog(ChildScrollActivity.this);  
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置风格为圆形进度条  
		mpDialog.setMessage("正在加载...");  
		mpDialog.setIndeterminate(false);//设置进度条是否为不明确  
		mpDialog.setCancelable(true);//设置进度条是否可以按退回键取消  
		mpDialog.show();
		new myThread(handler).start();
    }

    
    /**
     * 用于向ScrollLayout中添加需要显示的界面
     */
    public void initView() {
    	mRoot.removeAllViews();
    	newChildBornView=new NewChildBornView(this,profileid);
    	newChildBornView.setTag(FIRST_INTENT_TAG);
		newChildBornViewTwo=new NewChildBornView_Two(this,profileid);
		newChildBornViewTwo.setTag(SECOND_INTENT_TAG);
		smallOneChildView=new SmallOneChildView(this,profileid);
		smallOneChildView.setTag(THIRD_INTENT_TAG);
		smallOneChildViewTwo=new SmallOneChildView_Two(this,profileid);
		smallOneChildViewTwo.setTag(FOUR_INTENT_TAG);
		oneToTwoChildView=new OneToTwoChildView(this,profileid);
		oneToTwoChildView.setTag(FIVE_INTENT_TAG);
		oneToTwoChildViewTwo=new OneToTwoChildView_Two(this,profileid);
		oneToTwoChildViewTwo.setTag(SIX_INTENT_TAG);
		threeToSixChildView=new ThreeToSixChildView(this,profileid);
		threeToSixChildView.setTag(SEVEN_INTENT_TAG);
		threeToSixChildViewTwo=new ThreeToSixChildView_Two(this,profileid);
		threeToSixChildViewTwo.setTag(EIGHT_INTENT_TAG);
		mRoot.addView(newChildBornView);
		mRoot.addView(newChildBornViewTwo);
		mRoot.addView(smallOneChildView);
		mRoot.addView(smallOneChildViewTwo);
		mRoot.addView(oneToTwoChildView);
		mRoot.addView(oneToTwoChildViewTwo);
		mRoot.addView(threeToSixChildView);
		mRoot.addView(threeToSixChildViewTwo);
		
    }
    
    
    /**
     * 初始化本模块中进行表选择的列表
     */
    private void initExList(){
		ArrayList<Map<String, Object>> coll= new ArrayList<Map<String, Object>>();
        Map<String, Object> item= new HashMap<String, Object>();;
        item.put("name", "新生儿家庭访视记录表");
        coll.add(item);
        item= new HashMap<String, Object>();;
        item.put("name", "1岁以内儿童健康检查记录表");
        coll.add(item);
        item= new HashMap<String, Object>();;
        item.put("name", "1～2岁儿童健康检查记录表");
        coll.add(item);
        item= new HashMap<String, Object>();;
        item.put("name", "3～6岁儿童健康检查记录表");
        coll.add(item);
		listAccount.setAdapter(new SimpleAdapter(this, coll,R.layout.member_listview, new String[] { "name" },new int[] {R.id.txtFather}));
		// 去掉系统自带的按钮
		// 去掉系统自带的分隔线
		listAccount.setDivider(null);	
	}

	@Override
	public void onClick(View v) {
		if(v==leftBtn){
			new AlertDialog.Builder(ChildScrollActivity.this)
			.setIcon(R.drawable.alert_dialog_icon)
			.setTitle("您确定已经保存了要退出吗？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton) {
					ChildScrollActivity.this.finish();
					overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int whichButton) {

		                        /* User clicked Cancel so do some stuff */
		                   }
		                })
		                .create().show();
			
		}
		else if(v==rightBtn){
			System.out.println("pageIndex="+pageIndex);
			pageIndex=mRoot.getCurScreen();
			boolean result=false;
			int i=0;
			switch(pageIndex){
			case 0:
				result=newChildBornView.saveView()&&(newChildBornViewTwo.saveView());
				i=0;
			//	new saveThread(handler,"",2);
				break;
			case 1:
				result=(newChildBornView.saveView())&&(newChildBornViewTwo.saveView());
				i=0;
			//	new saveThread(handler,"",3);
				break;
			case 2:
				result=(smallOneChildView.saveView())&&(smallOneChildViewTwo.saveView());
				i=1;
			//	new saveThread(handler,"",4);
				break;
			case 3:
				result=(smallOneChildView.saveView())&&(smallOneChildViewTwo.saveView());
				i=1;
			//	new saveThread(handler,"",5);
				break;
			case 4:
				result=(oneToTwoChildView.saveView())&&(oneToTwoChildViewTwo.saveView());
				i=2;
			//	new saveThread(handler,"",6);
				break;
			case 5:
				result=(oneToTwoChildView.saveView())&&(oneToTwoChildViewTwo.saveView());
				i=2;
			//	new saveThread(handler,"",7);
				break;
			case 6:
				result=(threeToSixChildView.saveView())&&(threeToSixChildViewTwo.saveView());
				i=3;
			//	new saveThread(handler,"",8);
				break;
			case 7:
				result=(threeToSixChildView.saveView())&&(threeToSixChildViewTwo.saveView());
				i=3;
			//	new saveThread(handler,"",9);
				break;
			}
			if(result){
				Toast.makeText(this, childTable[i]+"表信息保存成功!", Toast.LENGTH_SHORT).show();
			}
			else{
				Toast.makeText(this, childTable[i]+"表信息保存失败!", Toast.LENGTH_SHORT).show();
			}
			
		}
	}
	
	
	/**
	 * @author chen
	 *在该线程中进行该模块界面初始化
	 */
	public class myThread extends Thread{  
		private Handler handler;

		public myThread(Handler handler){
			this.handler=handler;

		}
		
		public void run(){  
	        try  
	        {   
	        	Looper.getMainLooper();
	        	sleep(200); 
	        	
	        	newChildBornView.selectData();
	        	newChildBornViewTwo.findWeigt();
	        	smallOneChildView.findWeidget();
	        	smallOneChildViewTwo.findWeidget();
	        	oneToTwoChildView.findWeidget();
	        	oneToTwoChildViewTwo.findWeight();
	        	threeToSixChildView.findWeidget();
	        	threeToSixChildViewTwo.findWeidget();
	        	Message msg = handler.obtainMessage(); 
	        	msg.what=1;
	        	handler.sendMessage(msg); 
			}  
		    catch (Exception e)  
		    {  
		        e.printStackTrace();  
		    }  
		}  
	}
/*	public class saveThread extends Thread{
		private Handler handler;
		private String str;
		private int n;
		public saveThread(Handler handler,String s,int i){
			this.handler=handler;
			this.str=s;
			this.n=i;
		}
		public void run(Message msg){
			try{
				if(msg.what==2){
					newChildBornView.saveView();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}*/
	
	private final Handler handler = new Handler(Looper.getMainLooper()) {
    	public void handleMessage(Message msg) { // 处理Message，更新ListView 
    		if(msg.what==1){
    			newChildBornView.initView();
    			newChildBornViewTwo.addToView();
    			newChildBornViewTwo.initView();
    			smallOneChildView.addToView();
    			smallOneChildView.initView();
    			smallOneChildViewTwo.addToView();
    			smallOneChildViewTwo.initView();
    			oneToTwoChildView.addToView();
    			oneToTwoChildView.initView();
    			oneToTwoChildViewTwo.addToView();
    			oneToTwoChildViewTwo.initView();
    			threeToSixChildView.addToView();
    			threeToSixChildView.initView();
    			threeToSixChildViewTwo.addToView();
    			threeToSixChildViewTwo.initView();
    			mpDialog.cancel();
    		}
    	} 
    }; 
}

